package com.yoyo.itravel.web.controller;

import com.yoyo.itravel.bean.ResultInfo;
import com.yoyo.itravel.bean.SightOrder;
import com.yoyo.itravel.bean.User;
import com.yoyo.itravel.constants.UserConstant;
import com.yoyo.itravel.exceptions.SightOrderException;
import com.yoyo.itravel.service.ISightOrderService;
import com.yoyo.itravel.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 景点订单服务类
 *
 * @author HZX
 * @date 2020/5/19 21:07
 */
@Controller
@RequestMapping("/order")
public class SightOrderController {

    @Autowired
    private ISightOrderService sightOrderService;

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 通过用户id查询该用户的所有的景点订单
     */
    @RequestMapping("/queryAllByUserId")
    protected void queryAllByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得用户id
        User user = (User)request.getSession().getAttribute(UserConstant.USER);

        if (user == null) {
            // 用户未登录，跳转登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }
        try {

            List<SightOrder> sightOrders = sightOrderService.findAllByUserId(user.getUserId());

            // 返回订单集合到浏览器
            JSONUtils.writeValueToResponse(sightOrders, response);
        } catch (SightOrderException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 通过用户id查询该用户的所有的景点订单
     */
    @RequestMapping("/queryByOrderId")
    protected void queryByOrderId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得订单id
        String orderId = request.getParameter("orderId");
        User user = (User)request.getSession().getAttribute(UserConstant.USER);

        if (user == null) {
            // 用户未登录，跳转登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }
        try {

            SightOrder order = sightOrderService.findByOrderId(orderId);

            // 返回订单集合到浏览器
            JSONUtils.writeValueToResponse(order, response);
        } catch (SightOrderException e) {
            e.printStackTrace();
        }
    }


    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 通过用户id和订单状态查询该用户待付款的景点订单
     */
    @RequestMapping("/queryByUserIdAndStatus")
    protected void queryByUserIdAndStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得参数
        String orderStatus = request.getParameter("orderStatus");
        // 获得用户id
        User user = (User)request.getSession().getAttribute(UserConstant.USER);

        if (user == null) {
            // 用户未登录，跳转登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        try {
            List<SightOrder> sightOrders = sightOrderService.findByUserIdAndStatus(user.getUserId(), Integer.parseInt(orderStatus));
            // 返回订单集合到浏览器
            JSONUtils.writeValueToResponse(sightOrders, response);
        } catch (SightOrderException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 通过用户id和订单状态查询该用户待付款的景点订单
     */
    @RequestMapping("/deleteOrder")
    protected void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得参数
        String orderId = request.getParameter("orderId");
        ResultInfo info = new ResultInfo();

        try {
            boolean isSuccess = sightOrderService.deleteOrderByOrderId(orderId);
            if (isSuccess == true) {
                info.setFlag(true);
            }
            // 返回订单集合到浏览器
            JSONUtils.writeValueToResponse(info, response);
        } catch (SightOrderException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 取消订单
     */
    @RequestMapping("/cancelOrder")
    protected void cancelOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得参数
        String orderId = request.getParameter("orderId");
        ResultInfo info = new ResultInfo();

        try {
            // 执行退款的操作

            boolean isSuccess = sightOrderService.updateOrderStatus(orderId, 0);
            if (isSuccess == true) {
                info.setFlag(true);
            }
            // 返回订单集合到浏览器
            JSONUtils.writeValueToResponse(info, response);
        } catch (SightOrderException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }
}
